﻿using System;
using System.IO;
using FirstFloor.Documents.Controls.Commands;
using FirstFloor.Documents.IO;
using System.Windows.Controls;
using FirstFloor.Documents.IO.DotNetZip;

namespace SampleBrowser.Commands
{
    /// <summary>
    /// The command that opens an office document from disk, convert it and assigs it to a DocumentDataSource.
    /// </summary>
    public class OpenAndConvertLocalCommand
        : OpenFromDiskCommand
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="OpenAndConvertLocalCommand"/> class.
        /// </summary>
        public OpenAndConvertLocalCommand()
        {
            this.Filter = "All Documents (*.doc,*.docx,*.dwfx,*.ppt,*.pptx,*.pub,*.vdw,*.vsd,*.xls,*.xlsx,*.xps)|*.doc;*.docx;*.dwfx;*.ppt;*.pptx;*.pub;*.vdw;*.vsd;*.xls;*.xlsx;*.xps|Excel Sheets (*.xls,*.xlsx)|*.xls;*.xlsx|PowerPoint Presentations (*.ppt,*.pptx)|*.ppt;*.pptx|Publisher Documents (*.pub)|*.pub|Visio Documents (*.vdw,*.vsd)|*.vdw;*.vsd|Word Documents (*.doc,*.docx)|*.doc;*.docx|XPS Documents (*.dwfx,*.xps)|*.dwfx;*.xps|All Files (*.*)|*.*";
        }

        /// <summary>
        /// Creates a package reader for given stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns></returns>
        public override IPackageReader CreatePackageReader(Stream stream)
        {
            using(var converter = new DocumentConverter(stream)){
                var xpsStream = converter.ConvertToXps();

                return new DotNetZipPackageReader(xpsStream);
            }
        }
    }
}
